/**
 * 显示消息提示框
 * @param content 提示的标题
 */

 export function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content,
		duration: 2000
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}

export function getDeviceId(){
	return new Promise((resolve, reject) => {
		//#ifdef APP-PLUS
		if(plus){
			plus.android.requestPermissions(['android.permission.READ_PHONE_STATE'], function(e) {
				console.log(e)
				if (e.deniedAlways.length > 0) { //权限被永久拒绝
					console.log('Always Denied!!! ' + e.deniedAlways.toString());
					plus.android.requestPermissions(['android.permission.READ_PHONE_STATE'])
				}
				if (e.deniedPresent.length > 0) { //权限被临时拒绝
					// 弹出提示框解释为何需要读写手机储存权限，可再次调用plus.android.requestPermissions申请权限
					plus.android.requestPermissions(['android.permission.READ_PHONE_STATE'])
					console.log('Present Denied!!! ' + e.deniedPresent.toString());
				}
				if (e.granted.length > 0) { //权限被允许
					//调用依赖获取读写手机储存权限的代码
					console.log('Granted!!! ' + e.granted.toString());
					plus.device.getInfo({
						success: (e)=> {
							uni.setStorageSync('DeviceId', e.uuid)
							resolve(e)
						},
						fail: function(e) {
							console.log('fail' + e)
							reject(e)
						}
					});
				}
			}, function(e) {
				console.log('error' + e)
				reject(e)
			});
			
		}
		//#endif
	})
}
